package purchase.wf;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.jbpm.taskmgmt.exe.TaskMgmtInstance;




public class JbpmProcessHistoryServlet implements Servlet {

	private static Log log = LogFactory.getLog(JbpmProcessHistoryServlet.class);
	
	public ServletConfig getServletConfig() {
		return this.getServletConfig();
	}

	public String getServletInfo() {
		return this.getServletInfo();
	}

	public void init(ServletConfig servletConfig) throws ServletException {
		log.info("JbpmProcessHistoryServlet init......");
	}
	
	public void destroy() {
		log.info("JbpmProcessHistoryServlet destroy......");
	}

	/**
	 * 编码设置
	 * @param request
	 * @param response
	 */
	private void setReqRespEncoding(ServletRequest request,
			ServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
	}
	
	public void service(ServletRequest req, ServletResponse resp)
			throws ServletException, IOException {
		setReqRespEncoding(req, resp);
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;

		Long processInstanceId = Long.parseLong(request.getParameter("processInstanceId"));
		
		JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
		try {
			
			ProcessInstance processInstance = jbpmContext.getProcessInstance(processInstanceId);
			TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
			Collection<TaskInstance> taskInstances = taskMgmtInstance.getTaskInstances();
		
			TreeSet<ProcessHistoryInfo> processHistoryInfoList = new TreeSet<ProcessHistoryInfo>();
			
			for (Iterator<TaskInstance> ite = taskInstances.iterator(); ite.hasNext();) {
				TaskInstance taskInstance = ite.next();
				ProcessHistoryInfo processHistoryInfo = new ProcessHistoryInfo();
				
				processHistoryInfo.setTaskId(taskInstance.getId());
				processHistoryInfo.setTaskName(taskInstance.getName());
				processHistoryInfo.setCreateTime(taskInstance.getCreate());
				processHistoryInfo.setEndTime(taskInstance.getEnd());
				processHistoryInfo.setActorId(taskInstance.getActorId());
				
				processHistoryInfoList.add(processHistoryInfo);
			}
			
			request.setAttribute("processHistoryInfoList", processHistoryInfoList);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jbpmContext.close();
		}
		
		// forward
		request.getRequestDispatcher("/purchase/process_history_list.jsp").forward(request, response);
	}

}
